MBlockly = MBlockly || {};
MBlockly.Sensors = {
  sensorList : {
    'ULTRASONIC': 3,
    'LINEFOLLOW': 2,
    'LIGHTSENSOR': 6
  },
  gyroValue: {
    x: 0,
    y: 0,
    z: 0
  },
  axis: {
    x: 1,
    y: 2,
    z: 3,
  }
};

MBlockly.Sensors.openSensor = function(item) {
  var that = this;
  this['checkSensor-' + item] = setInterval(function() {
      MBlockly.Control.getSensorValue(item, that.sensorList[item], function(val){
        $('.' + item).text(val);

        if(item == "LINEFOLLOW") {
          sensorAction.setLinefollow(val);
        }

        if(item == "LIGHTSENSOR") {
          // 将光线的值传给灯泡
          sensorAction.setLight(val);
        }

        if(item == "ULTRASONIC") {
          sensorAction.setUltrasonic(val);
        }
      });
  }, 300);
}

MBlockly.Sensors.openUltrasonic = function(item) {

}

MBlockly.Sensors.openGyro = function(item) {
  var that = this;
  // for(var i in this.gyroValue) {
    setInterval(function() {
      var axis = that.axis[item];
      MBlockly.Control.getSensorValue('GYROSCOPE', 0, function(val){
        that.gyroValue[item] = val;
        sensorAction.setGyro(that.gyroValue);
        // $('.GYROSCOPE-' + item).text(val);
      }, axis);
    }, 300);
  // }
}

MBlockly.Sensors.closeSensor = function(item) {
  clearInterval(this['checkSensor-' + item]);
}

$(function() {
  // MBlockly.Sensors.openSensor('ULTRASONIC');
  MBlockly.Sensors.openSensor('LINEFOLLOW');
  // MBlockly.Sensors.openSensor('LIGHTSENSOR');
  // MBlockly.Sensors.openGyro("x");
  // MBlockly.Sensors.openGyro("y");
  // MBlockly.Sensors.openGyro("z");
});